<?php

namespace app\admin\model;

use think\Cache;
use think\Db;
use think\Model;

class QjQuestionLogModel extends Model
{

    public function addQuestionLog($data)
    {
        $res['success'] = false;
        $res['desc'] = '添加失败';
        $res['errorcode'] = '200022';
        $ret = Db::table('qj_question_log')->insert($data);
        if ($ret) {
            $id = Db::table('qj_question_log')->getLastInsID();
            $res['success'] = true;
            $res['desc'] = '添加成功';
            $res['data']['id'] = $id;
            $res['errorcode'] = '';
        }
        return $res;
    }

    public function getQuestionLogList($page, $pageSize)
    {

        $offset = ($page - 1) * $pageSize;
        if ($offset <= 0) {
            $offset = 0;
        }
        $ret = Db::table('qj_question_log')->limit($offset, $pageSize)->order('id desc')->select();
        $total = Db::table('qj_question_log')->count();
        $questionIds = [];
        foreach ($ret as $val) {
            if ($val['question_id']) {
                $questionIds[] = $val['question_id'];
            }
        }

        $questionList = Db::table('qj_question')->where('id', 'in', $questionIds)->order('id desc')->select();
        foreach ($questionList as $ques) {
            $questionListMap[$ques['id']] = $ques;
        }
        foreach ($ret as &$item) {
            if ($item['question_id']) {
                $item['content'] = isset($questionListMap[$item['question_id']]['content']) ? $questionListMap[$item['question_id']]['content'] : '';
            }
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $total_page = 0;
        if ($total) {
            $total_page = ceil($total / $pageSize);
        }
        $res['data']['list'] = $ret;
        $res['data']['total'] = $total;
        $res['data']['total_page'] = $total_page;
        return $res;
        return $res;
    }
}
